let reg={
    Username:/^[a-z]\w{6,9}$/,
    password:/^[a-zA-Z]\w{6,9}$/,
    name:/^[\u4E00-\u9FA5]{2,4}$/,
    sex:/^(男|女)$/,
    tel:/^[1][3-9][0-9]{9}$/
};

let oBtn2=document.getElementById('sign-in-btn2');
let oBtn3=document.getElementById('sign-in-btn3');
let oInp1=document.getElementById('sign-in-inp1');
let oInp2=document.getElementById('sign-in-inp2');
let oInp3=document.getElementById('sign-in-inp3');
let oInp4=document.getElementById('sign-in-inp4');
let oInp5=document.getElementById('sign-in-inp5');
let oFlag=true;
function test(obj){
    let oNext=obj.nextElementSibling;
    let str=obj.value;
    if(!str.trim()){
        oNext.innerHTML='不能为空';
        oNext.className='red';
        return false
    }
    if(reg[obj.name].test(str)){
        oNext.innerHTML='√';
        oNext.className='green';
        return true
    }else{
        oNext.innerHTML='×';
        oNext.className='red';
        return false
    }
}
oInp1.onblur=function(){
    test(this)
};

oInp2.onblur=function(){
    test(this)
};

oInp3.onblur=function(){
    let oNext1=oInp3.nextElementSibling;
    oNext1.innerHTML='√';
    oNext1.className='green';
};
oInp4.onblur=function(){
    test(this)
};

oInp5.onblur=function(){
    let oNext2=oInp5.nextElementSibling;
    oNext2.innerHTML='√';
    oNext2.className='green';

};
oBtn3.onclick=function () {
    let aA=document.getElementsByClassName('aaa');

    for(let i=0;i<aA.length;i++){
        if(!test(aA[i])){
            oFlag=false;
        }
    }
    if(!oFlag){
        oBtn2.disabled=true;
        return false
    }else{
        oBtn2.disabled=false
    }
};
let oUser=document.getElementById('sign-in-username');
let oPass=document.getElementById('sign-in-password');
let oBtn1=document.getElementById('sign-in-btn');
let oInp6=document.getElementById('sign-in-inp6');
let oInp7=document.getElementById('sign-in-inp7');
let oBtn4=document.getElementById('sign-in-btn4');

let json={
    "aUser":[{"user":"aaa","pass":111,"type":1},{"user":"bbb","pass":222,"type":2},{"user":"ccc","pass":333,"type":3}]
};
let str=JSON.stringify(json);
localStorage.setItem('data',str);
oBtn1.onclick=function () {
    let data=JSON.parse(localStorage.getItem('data'));
    let aUser=json.aUser;
    let sName=oUser.value;
    let sPass=oPass.value;

    for(let i=0;i<aUser.length;i++){
        if(aUser[i].user==sName){
            if(aUser[i].pass==sPass){
                window.location.href='index.html?type='+aUser[i].type;
                return;
            }else{
                alert('账号或密码错误');
                return;
            }
        }
    }
    alert('账号错误');
};
oBtn4.onclick=function () {
    let sName=oInp6.value;
    let sPass=oInp7.value;
    if(localStorage.getItem(sName)){
        alert('此账号已经存在');
    }else{
        localStorage.setItem(sName,sPass);
        json.aUser.push({
            "user":sName,"pass":sPass,
        });
        alert('成功')
    }
    console.log(json.aUser)
};
oInp6.onblur=function(){
    test(this)
};
oInp7.onblur=function(){
    test(this)
};




